package com.heihei.serial;

import com.heihei.util.FileUtilF;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class SerialNumbers {
    public static void main(String[] args) throws IOException {
        listCurrDir(new File("E:\\资源\\资源-p\\ASMR\\乡村菜馆-菜式教程_javbus_bfy\\02 第二期"));
    }

    public static void listCurrDir(File dir) throws IOException {
        File[] files = dir.listFiles();
        List<String> nameList = new ArrayList<>();
        //求file下的第二级的文件，做排序打印
        for (File file : files) {
            for (File child : file.listFiles()) {
                nameList.add(child.getName());
            }
//            String name = file.getName();
//            nameList.add(name);
        }
        nameList.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int numO1 = getNum(o1);
                int num02 = getNum(o2);
                return numO1 - num02;
            }
        });
        StringBuilder sb = new StringBuilder();
        for (String lineStr : nameList) {
            sb.append("- ").append(lineStr).append("\r\n");
        }
        File targetFile = new File(dir.getParentFile(), dir.getName() + ".txt");
        FileUtilF.writeTxtFile(sb.toString(), targetFile, "UTF-8");
    }

    public static int getNum(String str) {
        if(str == null || "".equals(str)) {
            return 0;
        }
        if(!isCharNum(str.charAt(0))) {
            return 0;
        }
        int i = 0;
        for (; i < str.length(); i++) {
            if(!isCharNum(str.charAt(i))) {
                break;
            }
        }
        if(i == 0) {
            return 0;
        }
        return Integer.valueOf(str.substring(0, i));
    }

    public static boolean isCharNum(Character ch) {
        if(ch == null) {
            return false;
        }
        char c = ch;
        return  c >= '0' && c <= '9';
    }

}
